<template>
  <div class="container">
    <!--left部分-->
    <div class="left-container box-shadow">
      <div class="list" v-for="item in data.artices">
        <div class="item" @click="goArticleDerail(item.articleId)">
          <div class="title">{{ item.articleTitle }}</div>
          <div class="flex-row" style="margin-top: 12px;">
            <svg-icon icon-class="day" />
            <div>{{ dayjs(item.updateDate).format('YYYY-MM-DD') }}</div>
          </div>
          <div class="content">{{ item.articleIntro }}</div>
          <div class="item-footer">
            <div class="tags">
              <div>{{ item.author }}</div>
              <el-divider direction="vertical" />
              <div class="flex-row" @click="likesClick(item.articleId)">
                <svg-icon icon-class="likes" v-if="item.isLike === 0" />
                <svg-icon icon-class="likesed" v-else />
                <div>{{ item.likes }}</div>
              </div>
              <el-divider direction="vertical" />
              <div class="flex-row">
                <svg-icon icon-class="comment" />
                <div>{{ item.commentCount }}</div>
              </div>
            </div>
            <div class="tags">
              <el-tag v-for="item in item.articleTags">{{ item }}</el-tag>
            </div>
          </div>
        </div>
      </div>
      <div class="page-container">
        <el-pagination v-model:current-page="data.page" v-model:page-size="data.size" small layout="prev, pager, next"
          :total="data.articeData.total"  @current-change="curChange()" />
      </div>

    </div>
    <!--right部分-->
    <div class="right-container ">
      <div class="box-shadow" style="background-color: #ffeded;">
        <div class="text-title">格言</div>
        <div style="text-align: center;line-height: 24px;">
          “<br>
          设计模式<br>
          设计模式是一种被反复使用的一种代码设计经验<br>
          用于提高代码的可重用性、可扩展性和可维护性<br>
          ”
        </div>
      </div>

      <div class="box-shadow" style="margin-top: 10px;">
        <div class="text-title">文章榜</div>
        <el-divider style="margin: 14px 0;" />
        <div>
          <div class="hot-item" v-for="(item, index) in rank" @click="goArticleDerail(item.articleId)">
            <div class="rank-no-gray rank-no">{{ index + 1 }}</div>
            <a class="rank-name">{{ item.articleTitle }}</a>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { articeList, likesRank } from "@/api/home"
import { likeArticle } from "@/api/article"
import SvgIcon from '@/components/SvgIcon.vue'
import { storeToRefs } from 'pinia'
import dayjs from 'dayjs'
import { ElMessage } from 'element-plus'
import { useUser } from '../stores/user'
const { userInfo } = storeToRefs(useUser())
import { useRouter } from 'vue-router'

const router = useRouter();


const data = reactive({
  // 文章列表
  articeData: {},
  artices: [],
  size: 5,
  page: 1,
  isdisabled: false
})

function curChange() {
  getData()
}
async function getData() {
  data.articeData = await articeList({ size: data.size, currentPage: data.page })

  data.artices = data.articeData.records.map(item => {
    item.articleTags = item.articleTags.split(",")
    return item
  })

}
getData()



const rank = ref([])
async function getLikesRank() {
  rank.value = await likesRank()
}
getLikesRank()



// 点赞
async function likesClick(articleId) {
  
  event.stopPropagation();
  const res = await likeArticle(articleId)
  if (res) {
    ElMessage.success(res)
  }
  data.artices = data.artices.map(item => {
    if (item.articleId === articleId && item.isLike === 0) {
      item.isLike = 1
      item.likes++
    }
    return item
  })
}


function goArticleDerail(articleId) {
  router.push(`/article?articleId=${articleId}`)
}


watch(userInfo, async (newVal) => {
  if(newVal === undefined) {
    getData();
  }
})

</script>

<style scoped lang="scss">
.container {
  display: flex;
  min-height: 700px;
  padding-bottom: 32px;
  margin: 10px;

}

.left-container {
  background-color: #fff;
  border-radius: 2px;
  width: 720px;
  position: relative;
  margin-right: 1rem;
  // padding: 0;
  // margin: 0 1rem 0 0;
  // list-style: none;

}

.page-container {
  display: flex;
  justify-content: center;
  margin: 22px;
}

.right-container {

  flex: 1;
}

.list {
  margin: 10px;
}

.item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 24px 0;
  margin: 0 24px;
  border-bottom: 1px solid #f0f0f2;


  .item-footer {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    width: 100%;

    .tags {
      display: flex;
      align-items: center;

      .el-tag {
        margin-left: 6px;
      }
    }

  }
}

.title {
  font-weight: 600;
  font-size: 20px;
  line-height: 22px;
  width: 100%;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
}

.content {
  font-size: 13px;
  line-height: 26px;
  margin: 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box; // 作为弹性伸缩盒子模型显示。
  -webkit-box-orient: vertical; // 设置伸缩盒子的子元素排列方式：从上到下垂直排列
  -webkit-line-clamp: 2;
}



.text-title {
  font-weight: 500;
  line-height: 2rem;
  text-align: center;
  padding-top: 10px;
  font-size: 16px;

}


.hot-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 8px;
  border-radius: 4px;
  line-height: 1.83rem;


  .rank-no {
    color: #8a919f;
    font-family: Archivo;
    text-align: left;
    min-width: 18px;
    line-height: 22px;
    margin: 0 10px;


  }




  .rank-name {
    color: #252933;
    display: inline-block;
    width: 100%;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }
}

.hot-item:nth-child(1) .rank-no {
  background: linear-gradient(180deg, #f64242 20%, rgba(246, 66, 66, .4) 80%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.hot-item:nth-child(2) .rank-no {
  background: linear-gradient(98deg, #ff7426 20%, rgba(255, 116, 38, .4) 80%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.hot-item:nth-child(3) .rank-no {
  background: linear-gradient(180deg, #ffac0c 20%, rgba(255, 172, 12, .4) 80%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
</style>
